<?php
class Options extends Action {
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		$me = check_login ( ADMIN_LOGIN_PAGE );
		$group = rqst ( 'group', 'base' );
		$Plugin_ID = rqst ( 'pid' );
		if (! empty ( $Plugin_ID )) {
			$plgMgr = PluginManager::getInstance ();
			$plgMgr->forceLoadPlugin ( $Plugin_ID );
		}
		$data = array ();
		$optM = new CoreOptionModel ();
		$opts = $optM->where ( array ('option_group' => $group ) )->retrieve ();
		$data ['options'] = apply_filter ( 'get_option_' . $group, array() );
		if (empty($data ['options'])) {
			$data ['options'] = $opts ? $opts->toArray ( 'option_name', 'option_value' ) : array ();
		}
		$data ['group'] = $group;
		bind ( 'get_core_option_group', array ($this, 'get_core_option_group' ), 0 );
		$data ['opt_groups'] = apply_filter ( 'get_core_option_group', array () );
		bind ( 'show_option_control', array ($this, 'show_option_control' ), 0, 3 );
		bind ( 'show_option_control', array ($this, 'show_smtp_option_control' ), 1, 3 );
		bind ( 'show_option_control', array ($this, 'show_sitemap_option_control' ), 1, 3 );
		bind ( 'show_option_control', array ($this, 'show_rss_option_control' ), 1, 3 );
		// 加载模板，这时可以方便的写一些js脚本在里边
		$data ['option_tpl'] = apply_filter ( "get_{$group}_option_tpl", false, $data ['options'] );
		return new SmartyView ( $data, 'dashboard/Options' );
	}
	public function get_core_option_group($groups) {
		$groups ['base'] = '<i class="icon-cog"></i>基本设置';
		$groups ['safe'] = '<i class="icon-fire"></i>安全设置';
		$groups ['thumb'] = '<i class="icon-picture"></i>缩略图与水印';
		$groups ['smtp'] = '<i class="icon-envelope"></i>邮件设置';
		$groups ['sitemap'] = '<i class="icon-globe"></i>网站地图';
		$groups ['rss'] = '<i class="icon-heart"></i>RSS订阅';
		return $groups;
	}
	public function show_option_control($ctlrs, $group, $options) {
		if ($group != 'base') {
			return $ctlrs;
		}
		$opts = array ();
		$opts [] = '<div class="control-group">
	          			<label for="site_name" class="control-label">网站名称</label>
	            		<div class="controls">
	              			<input class="span5" type="text" value="' . $options ['site_name'] . '" name="opt[base][site_name]" id="site_name"/>
	              			<strong class="help-inline refval">{$_OPT.site_name}</strong>	              			
	            		</div>
	          		</div>';
		$opts [] = '<div class="control-group ">
						<label for="site_url" class="control-label">网站URL</label>
						<div class="controls">
							<input class="span5" type="text" validate="{validate:{baseurl:true}}" value="' . $options ['site_url'] . '" name="opt[base][site_url]" id="site_url"/>
							<strong class="help-inline refval">{base}</strong>							
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="site_domain" class="control-label">网站主域</label>
						<div class="controls">
							<input class="span5" type="text" value="' . $options ['site_domain'] . '" name="opt[base][site_domain]" id="site_domain"/>
							<strong class="help-inline refval">{site_domain}</strong>							
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="site_keywords" class="control-label">默认关键词</label>
						<div class="controls">
							<input class="span5" type="text" value="' . $options ['site_keywords'] . '" name="opt[base][site_keywords]" id="site_keywords"/>
							<strong class="help-inline refval">{$_OPT.site_keywords}</strong>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="site_desc" class="control-label">默认网站描述</label>
						<div class="controls">
							<textarea class="span5" row="8" name="opt[base][site_desc]" id="site_desc">' . $options ['site_desc'] . '</textarea>
							<strong class="help-inline refval">{$_OPT.site_desc}</strong>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="link_types" class="control-label">友情链接分类</label>
						<div class="controls">
							<input class="span5" type="text" name="opt[base][link_types]" id="site_desc" value="' . $options ['link_types'] . '"/>
							<span class="help-inline">多个分类以\',\'分隔.</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="site_copyright" class="control-label">网站版权信息</label>
						<div class="controls">
							<textarea class="span5" row="8" name="opt[base][site_copyright]" id="site_copyright">' . $options ['site_copyright'] . '</textarea>
							<strong class="help-inline refval">{$_OPT.site_copyright}</strong>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="site_beian" class="control-label">网站备案编号</label>
						<div class="controls">
							<input class="span5" type="text" value="' . $options ['site_beian'] . '" name="opt[base][site_beian]" id="site_beian"/>
							<strong class="help-inline refval">{$_OPT.site_beian}</strong>
						</div>
					</div>';
		$opts = apply_filter ( 'get_base_option_control', $opts, $options );
		$ctlrs .= implode ( "\n", $opts );
		return $ctlrs;
	}
	public function show_smtp_option_control($ctlrs, $group, $options) {
		if ($group != 'smtp') {
			return $ctlrs;
		}
		$opts = array ();
		$type = $options ['smtp_type'];
		$type = empty ( $type ) ? 'smtp' : $type;
		$checked [$type] = ' checked="checked"';
		$opts [] = '<div class="control-group ">
						<label class="control-label">发送方式</label>
						<div class="controls">
							<label class="radio inline"><input type="radio" value="smtp" name="opt[smtp][smtp_type]"' . $checked ['smtp'] . '/>SMTP</label>
							<label class="radio inline"><input type="radio" value="mail" name="opt[smtp][smtp_type]"' . $checked ['mail'] . '/>mail函数</label>
							<label class="radio inline"><input type="radio" value="sendmail" name="opt[smtp][smtp_type]"' . $checked ['sendmail'] . '/>sendmail</label>
							<label class="radio inline"><input type="radio" value="qmail" name="opt[smtp][smtp_type]"' . $checked ['qmail'] . '/>qmail</label>
						</div>
					</div>';
		$type = $options ['smtp_secure'];
		$type = empty ( $type ) ? 'none' : $type;
		$secure [$type] = ' checked="checked"';
		$opts [] = '<div class="control-group ">
						<label class="control-label">安全链接</label>
						<div class="controls">
							<label class="radio inline"><input type="radio" value="" name="opt[smtp][smtp_secure]"' . $secure ['none'] . '/>不使用安全链接</label>
							<label class="radio inline"><input type="radio" value="ssl" name="opt[smtp][smtp_secure]"' . $secure ['ssl'] . '/>使用SSL</label>
							<label class="radio inline"><input type="radio" value="tls" name="opt[smtp][smtp_secure]"' . $secure ['tls'] . '/>使用TLS</label>							
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="smtp_host" class="control-label">主机</label>
						<div class="controls">
							<input class="span5" type="text" value="' . $options ['smtp_host'] . '" name="opt[smtp][smtp_host]" id="smtp_host"/>
							<span class="help-inline">域名或IP址</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="smtp_port" class="control-label">端口</label>
						<div class="controls">
							<input class="span5" type="text" validate="{validate:{digits:true,messages:{digits:\'请输入合法的端口号\'}}}" value="' . $options ['smtp_port'] . '" name="opt[smtp][smtp_port]" id="smtp_port"/>
							<span class="help-inline">默认为25</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="smtp_from" class="control-label">发件人名称</label>
						<div class="controls">
							<input class="span5" type="text" value="' . $options ['smtp_from'] . '" name="opt[smtp][smtp_from]" id="smtp_from"/>
							<span class="help-inline">显示在发件人一栏</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="smtp_reply" class="control-label">回复地址</label>
						<div class="controls">
							<input class="span5" validate="{validate:{email:true,messages:{email:\'请输入合法的邮件地址\'}}}" type="text" value="' . $options ['smtp_reply'] . '" name="opt[smtp][smtp_reply]" id="smtp_reply"/>
							<span class="help-inline">用于接收回复的邮件地址,一般与邮件账户相同.</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="smtp_user" class="control-label">邮件账户</label>
						<div class="controls">
							<input class="span5" type="text" value="' . $options ['smtp_user'] . '" name="opt[smtp][smtp_user]" id="smtp_user"/>
							<span class="help-inline">登录邮件系统的账户</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="smtp_passwd" class="control-label">账户密码</label>
						<div class="controls">
							<input class="span5" type="password" name="opt[smtp][smtp_passwd]" id="smtp_passwd" value="' . $options ['smtp_passwd'] . '"/>
							<span class="help-inline">登录邮件系统的密码</span>
						</div>
					</div>';
		if (! empty ( $options ['smtp_host'] )) {
			$opts [] = '<div class="control-group ">
						<label for="smtp_test" class="control-label">测试邮件地址</label>
						<div class="controls">
							<input class="span5" type="text" id="smtp_test" value=""/>
							<a href="#" id="test_mailer_setting">点击此处进行设置测试</a>	
						</div>
					</div>';
		}
		$ctlrs .= implode ( "\n", $opts );
		return $ctlrs;
	}
	
	public function show_sitemap_option_control($ctlrs, $group, $options) {
		if ($group != 'sitemap') {
			return $ctlrs;
		}
		$opts [] = '<div class="control-group ">
						<label for="sitemap_cache" class="control-label">缓存时间</label>
						<div class="controls">
							<input class="span2"  validate="{validate:{digits:true,messages:{digits:\'请输入合法的缓存时间\'}}}" type="text" name="opt[sitemap][sitemap_cache]" id="sitemap_cache" value="' . $options ['sitemap_cache'] . '"/>
							<span class="help-inline">如果开启了系统缓存，网站地图将被缓存，单位为秒,0表示不缓存。</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="sitemap_tpl" class="control-label">网站地图模板</label>
						<div class="controls">
							<input class="span3" type="text" name="opt[sitemap][sitemap_tpl]" id="sitemap_tpl" value="' . $options ['sitemap_tpl'] . '"/>
							<span class="help-inline">如果不填写将使用系统默认模板system/sitemap.tpl。</span>
						</div>
					</div>';
		$ctlrs .= implode ( "\n", $opts );
		return $ctlrs;
	}
	
	public function show_rss_option_control($ctlrs, $group, $options) {
		if ($group != 'rss') {
			return $ctlrs;
		}
		if ($options ['rss_enabled']) {
			$en [1] = ' checked="checked"';
		} else {
			$en [0] = ' checked="checked"';
		}
		$opts [] = '<div class="control-group ">
						<label class="control-label">启用RSS订阅</label>
						<div class="controls">
							<label class="radio inline"><input type="radio" value="1" name="opt[rss][rss_enabled]"' . $en [1] . '/>启用</label>
							<label class="radio inline"><input type="radio" value="0" name="opt[rss][rss_enabled]"' . $en [0] . '/>停用</label>														
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="rss_cache" class="control-label">缓存时间</label>
						<div class="controls">
							<input class="span2"  validate="{validate:{digits:true,messages:{digits:\'请输入合法的缓存时间\'}}}" type="text" name="opt[rss][rss_cache]" id="rss_cache" value="' . $options ['rss_cache'] . '"/>
							<span class="help-inline">如果开启了系统缓存，RSS订阅将被缓存，单位为秒,0表示不缓存。</span>
						</div>
					</div>';
		$opts [] = '<div class="control-group ">
						<label for="rss_tpl" class="control-label">RSS订阅模板</label>
						<div class="controls">
							<input class="span3" type="text" name="opt[rss][rss_tpl]" id="rss_tpl" value="' . $options ['rss_tpl'] . '"/>
							<span class="help-inline">如果不填写将使用系统默认模板system/rss.tpl。</span>
						</div>
					</div>';
		$ctlrs .= implode ( "\n", $opts );
		return $ctlrs;
	}
}